package org.greenwire.islandharvester.domain;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.greenwire.islandharvester.result.Result;

/**
 * 快乐岛主客船对象
 * @author wei.zhongw
 *
 */
public class Boat implements Harvestable {
	
	private String id;
	
	/**
	 * 距离下次到达的时长，单位秒
	 */
	private String time;
	
	private String visitorNum;
	
	private String maxVisitorNum;
	
	/**
	 * 船的等级  1、木筏   2、木船  3、橡皮艇 。。。
	 */
	private String boatId;
	
	/**
	 * onTheRoad | arrive_1
	 */
	private String state;	
	
	private final String HARVEST_URL = "http://island.hapyfish.com/api/receiveboat?positionId=";
	
	@Override
	public boolean isHarvestable(){
		return true;
	}
	
	@Override
	public String getType(){
		return "boat";
	}

	@Override
	public boolean available4HarvestImmediately() {		
		return StringUtils.startsWith(getState(), "arrive_");
	}
	
	@Override
	public boolean available4HarvestLater(){
		return !StringUtils.equals("0", getTime());
	}

	@Override
	public String getHarvestUrl(String ownerUid) {
		return HARVEST_URL + getId();
	}
	
	@Override
	public String getNextAvailableTime(){
		return getTime();
	}

	@Override
	public Result parseResult(String resultStr) {
		Result result = new Result();
		JSONObject jResult = JSONObject.fromObject(resultStr);
		try {
			if (StringUtils.equals("-1", jResult.getString("status"))) {
				result.setSuccess(false);
				result.setResultStr("FAILED! Server return : "
						+ jResult.getString("content"));
			}
		} catch (JSONException je) {
			result.setSuccess(true);
		}
		
		return result;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getVisitorNum() {
		return visitorNum;
	}

	public void setVisitorNum(String visitorNum) {
		this.visitorNum = visitorNum;
	}

	public String getMaxVisitorNum() {
		return maxVisitorNum;
	}

	public void setMaxVisitorNum(String maxVisitorNum) {
		this.maxVisitorNum = maxVisitorNum;
	}

	public String getBoatId() {
		return boatId;
	}

	public void setBoatId(String boatId) {
		this.boatId = boatId;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
}
